package com.zat.mes.andrmes.dofun.work

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.google.gson.JsonObject
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.customerview.BottomSignCommDialog
import com.zat.mes.andrmes.data.Itemm
import com.zat.mes.andrmes.data.ParantTypeItem
import com.zat.mes.andrmes.data.ScheduClassBean
import com.zat.mes.andrmes.dofun.work.vm.BadFlatModel
import com.zat.mes.andrmes.dofun.work.vm.YarnInModel
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_bad_flat.*
import kotlinx.android.synthetic.main.activity_on_site_detail.*
import kotlinx.android.synthetic.main.activity_yarn_in.*
import kotlinx.android.synthetic.main.activity_yarn_in.llBtnScheduNo
import kotlinx.android.synthetic.main.activity_yarn_in.tvScheduNo
import kotlinx.android.synthetic.main.layout_title.*
import org.json.JSONObject

/**
 * 新增废片
 */
class BadFlatActivity : BaseActivity() {

    private var empIds: String? = null
    var badFlatModel: BadFlatModel? = null
    var schedu: Itemm? = null
    var classId: Long = 0
    var classBean: MutableList<ScheduClassBean>? = null
    var empBean: MutableList<ScheduClassBean>? = null
    override fun init() {
        initView()
        initData()
    }

    private fun initData() {
        badFlatModel = ViewModelProvider(this).get(BadFlatModel::class.java)
        badFlatModel?.getScheduClass()?.observe(this, Observer {
            classBean = it

        })
        badFlatModel?.getScheduEmp()?.observe(this, Observer {
            empBean = it
        })
        badFlatModel?.getBadFlat()?.observe(this, Observer {
            closeLoading()
            showToast(it)
        })

        llClass.setOnClickListener {
            showBodyDialog(classBean, 0)
        }
        llEmp.setOnClickListener {
            showEmpDialog(empBean, 0)
        }
    }

    private fun initView() {
        setContentView(R.layout.activity_bad_flat)
        tVtitle.text = "废片"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        llBtnScheduNo.setOnClickListener {
            startActivityForResult(Intent(this, SelectScheduActivity::class.java), 0)
        }
        btnBadFlat.setOnClickListener {
            if (schedu == null) {
                showToast("请选择排版")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etFname.text.toString())) {
                    showToast("请输入废片名")
                    return@setOnClickListener
            }
            if (TextUtils.isEmpty(tbClass.text.toString())) {
                showToast("请选择班组")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(tbEmp.text.toString())) {
                showToast("请选择人员")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etSizeBadFlat.text.toString())) {
                showToast("请输入数量")
                return@setOnClickListener
            }
            showLoading()
            val json = JSONObject()
            json.put("schedulNumber", schedu?.schedNo)
            json.put("empIds", empIds)
            json.put("quantity", etSizeBadFlat.text.toString())
            json.put("classId", classId)
            json.put("scrapsName",etFname.text.toString())
            badFlatModel?.postBadFlat(json.toString())
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val item = data?.getParcelableExtra("schedu") as Itemm
            schedu = item
            tvScheduNo.text = "排班号：" + item?.schedNo
            badFlatModel?.getScheduClass(schedu?.scheduId)

        }
    }

    /**
     * 班组 弹框
     */
    fun showBodyDialog(list: MutableList<ScheduClassBean>?, selectPosition: Int) {
        val strList: MutableList<String> = ArrayList()
        if (list != null) {
            for (type in list) {
                strList.add(type.className)
            }
        }
        BottomSignCommDialog(
            this,
            strList,
            selectPosition,
            object : BottomSignCommDialog.OnBottomSelectListener {
                override fun onBottomSelect(position: Int) {
                    tbClass.text = list?.get(position)?.className
                    classId = list?.get(position)?.classId!!
                    badFlatModel?.getScheduEmp(schedu?.scheduId, classId)
                }

            })
    }

    /**
     * 员工 弹框
     */
    fun showEmpDialog(list: MutableList<ScheduClassBean>?, selectPosition: Int) {
        val strList: MutableList<String> = ArrayList()
        if (list != null) {
            for (type in list) {
                strList.add(type.morningEmpName + "," + type.nightEmpName)
            }
        }
        BottomSignCommDialog(
            this,
            strList,
            selectPosition,
            object : BottomSignCommDialog.OnBottomSelectListener {
                override fun onBottomSelect(position: Int) {
                    tbEmp.text =
                        list?.get(position)?.morningEmpName + "," + list?.get(position)?.nightEmpName
                    empIds =
                        list?.get(position)?.morningEmpId.toString() + "," + list?.get(position)?.nightEmpId.toString()
                }

            })
    }
}
